﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Scaling : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public string Id { get; set; }
        public MaxSize MaxSize { get; set; }
        public Scale Scale { get; set; }

        public Scaling()
        { }

        public Scaling(XmlNode node)
        {
            try
            {
                this.Id = XmlHelper.GetAttribute(node, "Id");

                if (node.SelectSingleNode("MaxSize") != null)
                {
                    this.MaxSize = new MaxSize(node.SelectSingleNode("MaxSize"));
                }

                if (node.SelectSingleNode("Scale") != null)
                {
                    this.Scale = new Scale(node.SelectSingleNode("Scale"));
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode scalingNode = XmlHelper.AddNode(commandUINode, "Scaling");
                XmlHelper.AddAttribute(scalingNode, "Id", this.Id);

                if (this.MaxSize != null)
                {
                    this.MaxSize.AddNode(commandUINode.SelectSingleNode("MaxSize"));
                }

                if (this.Scale != null)
                {
                    this.Scale.AddNode(commandUINode.SelectSingleNode("Scale"));
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Scaling");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "MaxSize", "Scale" }, true);

                if (this.MaxSize != null)
                {
                    node.ContextMenuStrip.Items[0].Enabled = false;
                    this.MaxSize.BuildTreeViewElement(node);
                }

                if (this.Scale != null)
                {
                    node.ContextMenuStrip.Items[1].Enabled = false;
                    this.Scale.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is MaxSize) && !(child is Scale))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (child is MaxSize)
                    {
                        this.MaxSize = (MaxSize)child;
                    }
                    else
                    {
                        this.Scale = (Scale)child;
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is MaxSize)
                {
                    this.MaxSize = null;
                }
                else if (o is Scale)
                {
                    this.Scale = null;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
